package cn.com.tuns.assess.qsb.frame.widget.dialog;


import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.tuns.assess.qsb.R;
import cn.com.tuns.assess.qsb.frame.util.AndroidUtils;
import cn.com.tuns.assess.qsb.frame.util.DimenUtils;


public class Dialog extends android.app.Dialog implements OnClickListener {

	private LinearLayout contentView;

	public Dialog(Context context) {
		this(context, AndroidUtils.getDialogTheme(context, R.attr.dialogStyle));
	}

	public Dialog(Context context, int theme) {
		this(context, theme, 0.86f);
	}

	public Dialog(Context context, float widthProportion) {
		this(context, AndroidUtils.getDialogTheme(context, R.attr.dialogStyle), widthProportion);
	}

	public Dialog(Context context, int theme, float dialogWidthRatio) {
		super(context, theme);
		setCanceledOnTouchOutside(true);
		contentView = new LinearLayout(context);
		contentView.setOrientation(LinearLayout.VERTICAL);
		super.setContentView(contentView);
		if (dialogWidthRatio > 0) {
			adjustDialogWidth(dialogWidthRatio);
		}
	}

	public void adjustDialogWidth(float dialogWidthRatio) {
		final Window window = getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (DimenUtils.getWindowWidth(window) * dialogWidthRatio);
		window.setAttributes(lp);
	}

	public void setTitle(String title) {
		if (title != null) {
			setContentView(R.layout.dialog_title);
			TextView titleView = findViewById(R.id.dialog_title);
			titleView.setText(title);
		}
	}
	public void setContent(String title, String content) {
		if (title!=null&&content != null) {
			setContentView(R.layout.dialog_title);
			TextView titleView = findViewById(R.id.dialog_title);
			TextView contentTView= findViewById(R.id.dialog_content);
			contentTView.setVisibility(View.VISIBLE);
			titleView.setText(title);
			contentTView.setText(content);
		}

	}

	@Override
	public void setContentView(int layoutRes) {
		getLayoutInflater().inflate(layoutRes, contentView);
	}

	@Override
	public void setContentView(View view) {
		contentView.addView(view);
	}

	public Dialog setButton(String confirmButtonText) {
		Button button = new Button(getContext(), null, R.attr.dialogButtonStyle);
		button.setId(R.id.dialog_button_confirm);
		button.setText(confirmButtonText);
		button.setOnClickListener(this);
		contentView.addView(button);
		return this;
	}

	public Dialog setButton(String cancelButtonText, String confirmButtonText) {
		LinearLayout btnLayout = new LinearLayout(getContext());
		Button button = new Button(getContext(), null, R.attr.dialogButtonLeftStyle);
		button.setId(R.id.dialog_button_cancel);
		button.setText(cancelButtonText);
		button.setOnClickListener(this);
		btnLayout.addView(button, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
		button = new Button(getContext(), null, R.attr.dialogButtonRightStyle);
		button.setId(R.id.dialog_button_confirm);
		button.setText(confirmButtonText);
		button.setOnClickListener(this);
		btnLayout.addView(button, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
		contentView.addView(btnLayout);
		return this;
	}

	public Dialog setDefaultButton() {
		setButton("取消", "确定");
		return this;
	}

	public void showStable() {
		setCanceledOnTouchOutside(false);
		setCancelable(false);
		show();
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.dialog_button_cancel) {
			onCancel();
		} else if (view.getId() == R.id.dialog_button_confirm) {
			onConfirm();
		}
	}

	public void onConfirm() {
		dismiss();
		afterConfirm();
	}

	public void onCancel() {
		dismiss();
		afterCancel();
	}

	public void afterConfirm() {
		// 2020-07-16
//		Intent intent=new Intent();
//		intent.setClass(AppContext.applicationContext,LoginActivity.class);
//		AppContext.getInstance().startActivity(intent);
	}

	public void afterCancel() {
	}

}
